package cn.qzuie.smart.module.dorm.controller.admin.bills;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.CommonResult;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;
import static cn.qzuie.smart.framework.common.pojo.CommonResult.success;

import cn.qzuie.smart.framework.excel.core.util.ExcelUtils;

import cn.qzuie.smart.framework.apilog.core.annotation.ApiAccessLog;
import static cn.qzuie.smart.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.qzuie.smart.module.dorm.controller.admin.bills.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.bills.BillsDO;
import cn.qzuie.smart.module.dorm.service.bills.BillsService;

@Tag(name = "管理后台 - 账单")
@RestController
@RequestMapping("/dorm/bills")
@Validated
public class BillsController {

    @Resource
    private BillsService billsService;

    @PostMapping("/create")
    @Operation(summary = "创建账单")
    @PreAuthorize("@ss.hasPermission('dorm:bills:create')")
    public CommonResult<Long> createBills(@Valid @RequestBody BillsSaveReqVO createReqVO) {
        return success(billsService.createBills(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账单")
    @PreAuthorize("@ss.hasPermission('dorm:bills:update')")
    public CommonResult<Boolean> updateBills(@Valid @RequestBody BillsSaveReqVO updateReqVO) {
        billsService.updateBills(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dorm:bills:delete')")
    public CommonResult<Boolean> deleteBills(@RequestParam("id") Long id) {
        billsService.deleteBills(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除账单")
                @PreAuthorize("@ss.hasPermission('dorm:bills:delete')")
    public CommonResult<Boolean> deleteBillsList(@RequestParam("ids") List<Long> ids) {
        billsService.deleteBillsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dorm:bills:query')")
    public CommonResult<BillsRespVO> getBills(@RequestParam("id") Long id) {
        BillsDO bills = billsService.getBills(id);
        return success(BeanUtils.toBean(bills, BillsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账单分页")
    @PreAuthorize("@ss.hasPermission('dorm:bills:query')")
    public CommonResult<PageResult<BillsRespVO>> getBillsPage(@Valid BillsPageReqVO pageReqVO) {
        PageResult<BillsDO> pageResult = billsService.getBillsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出账单 Excel")
    @PreAuthorize("@ss.hasPermission('dorm:bills:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBillsExcel(@Valid BillsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BillsDO> list = billsService.getBillsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "账单.xls", "数据", BillsRespVO.class,
                        BeanUtils.toBean(list, BillsRespVO.class));
    }

}